import { post } from '@/api/http'
import { ROLE_LIST } from '@/api/role'
import { Role, RoleId } from '@/types/role.d'

const roles = ref<Role[]>([])

export const useRole = () => {

  const getRoles = async () => {
    if (roles.value.length) return
    const { body } = await post<Role[]>({
      url: ROLE_LIST,
    })
    roles.value = body
  }

  const getRole = computed(() => (roleId: string) => roles.value.find((role) => role.roleId == roleId))

  return {
    RoleId,

    roles,
    getRole,
    getRoles
  }
}
